package com.hongyi.modelcontest.core.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {

    /**
     * 获取当前系统时间
     */
    public static Date getNow() {
        return new Date();
    }

    /**
     * 加 天数
     */
    public static Date getDateNextDay(Integer day) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, day);
        return calendar.getTime();
    }

    /**
     * 加 年
     */
    public static Date getDateNextYear(Integer year) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.YEAR, year);
        return calendar.getTime();
    }


    /**
     * 格式化时分秒 为00：00：00
     */
    public static Date parseDayMin(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat();
        String str = parseString(date, "yyyy-MM-dd") + " 00:00:00";
        return parse(str, "yyyy-MM-dd HH:mm:ss");
    }

    public static Date parseDayMax(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat();
        String str = parseString(date, "yyyy-MM-dd") + " 23:59:59";
        return parse(str, "yyyy-MM-dd HH:mm:ss");
    }

    /**
     * 格式化时分秒 为00：00
     */
    public static Date parseMinute(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat();
        String str = parseString(date, "yyyy-MM-dd") + " 00:00";
        return parse(str, "yyyy-MM-dd HH:mm");
    }

    /**
     * 日期转字符串
     */
    public static String parseString(Date date, String patten) {
        SimpleDateFormat sdf = new SimpleDateFormat(patten);
        return sdf.format(date);
    }

    /**
     * 日期转字符串
     */
    public static String toString(LocalDate date) {
        DateTimeFormatter sdf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        return sdf.format(date);
    }

    /**
     * 将字符串转为Date
     */
    public static Date parse(String strDate, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        try {
            return sdf.parse(strDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Date getDateNextHour(int hour) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.HOUR, hour);
        return calendar.getTime();
    }


    public static String toString(LocalDateTime date) {
        DateTimeFormatter sdf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return sdf.format(date);
    }
}